package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import users.Customer;

public class PopularItems extends JPanel{
	
	private Customer cust;
	private JPanel bPanel = new JPanel(new GridLayout(3,3,10,10));
	private JPanel tPanel = new JPanel(new FlowLayout());;
	private JTextField dText, nText;
	private JButton show;
	private DefaultTableModel iModel;
	private JTable iTable;
	private String dateString = null;
	private int number = 0;
	
	
	public PopularItems(Customer man) {
		
		this.cust = man;
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
	
		buttonPanel();
		
		tablePanel();
	}

	// panel with all the input fields
	private void buttonPanel() {
		
		bPanel.add(new JLabel("Enter the Date: (YYYY-MM-DD)"));
		dText = new JTextField();
        dText.setPreferredSize(new Dimension(300, 30));
        bPanel.add(dText);
		
		bPanel.add(new JLabel("Enter the Number of Items to Display:"));
		nText = new JTextField();
        nText.setPreferredSize(new Dimension(300, 30));
        bPanel.add(nText);
        
        bPanel.add(new JLabel("Press Button To Display Items:"));
		show = new JButton("Show Items");
		show.addActionListener(new generateReport());
        show.setPreferredSize(new Dimension(200, 30));
        bPanel.add(show);
        
        add(bPanel);
	}
	
	// panel with the table
	private void tablePanel() {
		// create the table
		iModel= new DefaultTableModel();
		iTable = new JTable(iModel);
		
		// set the column names
		iModel.addColumn("Title");
		iModel.addColumn("Company");
		iModel.addColumn("Current Stock");
		iModel.addColumn("Items Sold");
		
		JScrollPane scrollPane = new JScrollPane(iTable);
        tPanel.add(scrollPane,BorderLayout.CENTER);
        
        add(tPanel);
	}
	
	// action listener for the show button
	private class generateReport implements ActionListener {

		ResultSet items = null;
		int count = 0;
		String title = null, company = null;
		int stock = 0, quantity = 0;
		
		public void actionPerformed(ActionEvent arg0) {
			dateString = dText.getText();
			number = Integer.valueOf(nText.getText());
			
			//refresh the popular items table
			tPanel.removeAll();
			tablePanel();
			
			// get the popular items
			items = cust.getPopularItems(dateString, number);
			
			// extract the data and put it into the table
			try {
				while((count < number) && items.next()){
					
					title = items.getString(1);
					company = items.getString(2);
					stock = items.getInt(3);
					quantity = items.getInt(4);
					
					iModel.addRow(new Object[]{title, company, stock, quantity});
					
					count++;
				}
				
				count = 0;
				tPanel.validate();
			} catch (SQLException e) {
				System.out.println("Could not extract popular items from a result set.");
				e.printStackTrace();
			}
		}
	}
}
